package com.smooth.site.sx.pc.offer.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smooth.common.pc.interceptor.PcAccountContext;
import com.smooth.common.site.common.enums.SiteCodeEnums;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.rsp.ReturnResult;
import com.smooth.common.site.common.utils.StringUtils;
import com.smooth.dao.gen.bean.BizCompany;
import com.smooth.dao.gen.bean.BizJobApplay;
import com.smooth.dao.gen.bean.BizOffer;
import com.smooth.dao.gen.bean.BizStudent;
import com.smooth.site.sx.mag.biz.applyjob.service.BizJobApplayService;
import com.smooth.site.sx.mag.biz.offer.service.BizOfferService;
import com.smooth.site.sx.pc.job.enums.ApplyJobStatus;
import com.smooth.site.sx.pc.offer.enums.OfferStatus;
import com.smooth.site.sx.pc.offer.req.OfferCreateReq;
import com.smooth.site.sx.pc.offer.req.OfferReq;
import com.smooth.site.sx.pc.offer.service.PcOfferService;
import com.smooth.sys.pc.vo.ListRsp;

@Controller
@RequestMapping(value = "/pc/c")
public class PcCompOfferController {
	@Autowired
	PcOfferService pcOfferService;
	@Autowired
	BizJobApplayService bizJobApplayService;
	/**
	 * 学生注册
	 * 
	 * @param studenRegVo
	 * @param result
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = { "comp/offer/upload/pdf" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult uploadPdf(@Valid OfferReq offerReq, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		if(StringUtils.isBlank(offerReq.getPdfUrl())) {
			throw new SiteException(SiteCodeEnums.param_error, "pdf地址不能为空");
		}
		BizOffer bizOffer = pcOfferService.addAttachPdf(offerReq.getOfferId(), offerReq.getPdfUrl());
		returnResult.setData(bizOffer);
		return returnResult;
	}
	@Autowired
	BizOfferService bizOfferService;
	@RequestMapping(value = { "comp/offer/upd/status" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult compUpdStatus(@Valid OfferReq offerReq, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizOffer bizOffer = bizOfferService.get(offerReq.getOfferId());
		
		BizCompany secBizCompany = (BizCompany)PcAccountContext.getObj(BizCompany.class);
		if(!secBizCompany.getId().equals(bizOffer.getCompId())) {
			throw new SiteException(SiteCodeEnums.param_error, "参数错误");
		}
		
		OfferStatus offerStatus = OfferStatus.getStatus(offerReq.getOfferStatus());
		if(OfferStatus.failed.equals(offerStatus) || OfferStatus.toconfirm.equals(offerStatus)) {
			bizOffer = pcOfferService.statusBizoffer(offerReq.getOfferId(), OfferStatus.getStatus(offerReq.getOfferStatus()));
			returnResult.setData(bizOffer);
		}else {
			throw new SiteException(SiteCodeEnums.param_error, "状态错误");
		}
		return returnResult;
	}
	
	
	@RequestMapping(value = {"comp/offer/createOffer"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult createOffer(OfferCreateReq offerCreateReq,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizCompany secBizCompany = (BizCompany)PcAccountContext.getObj(BizCompany.class);
		
		BizJobApplay bizJobApplay = bizJobApplayService.get(offerCreateReq.getApplyId());
		if(!bizJobApplay.getCompId().equals(secBizCompany.getId())) {
			throw new SiteException(SiteCodeEnums.param_error,"权限不足");
		}
		
		ApplyJobStatus applyJobStatus = ApplyJobStatus.getStatus(bizJobApplay.getApplayStatus());
		if(ApplyJobStatus.applysuccess.equals(applyJobStatus) || ApplyJobStatus.compsendaccept.equals(applyJobStatus)) {
			BizOffer bizOffer = pcOfferService.createOffer(bizJobApplay,offerCreateReq.getPdfUrl());
			returnResult.setData(bizOffer);
		}else {
			throw new SiteException(SiteCodeEnums.param_error,offerCreateReq.getApplyId()+"只有申请成功才能生成offer");
		}
		return returnResult;
	}
	
	
	@RequestMapping(value = { "comp/offer/list" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult list(Integer offerStatus, HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizCompany secBizCompany = (BizCompany)PcAccountContext.getObj(BizCompany.class);
		OfferStatus offerStatusReq = OfferStatus.getStatus(offerStatus);
		
		ListRsp<BizOffer> blist = pcOfferService.queryCompBizoffers(secBizCompany.getId(), offerStatusReq);
		returnResult.setData(blist);
		
		return returnResult;
	}

}
